import os.path
from tools.funciton import replace_column_values, read_xlsx_file
from config.configs import GOAT_ERP_HOST, GOAT_ERP_PORT, DATA_PATH
import pymongo
import requests

w1 = pymongo.MongoClient(GOAT_ERP_HOST, GOAT_ERP_PORT).goat_warehouse_w1
w2 = pymongo.MongoClient(GOAT_ERP_HOST, GOAT_ERP_PORT).goat_warehouse_w2
dev = pymongo.MongoClient(GOAT_ERP_HOST, GOAT_ERP_PORT).goat_warehouse_dev


def get_token(domain):
    """获取最新token"""
    if domain != 'dev':
        url = f'https://{domain}-api.hkmotivate.com/auth/login'
        param = {"mobile": "Dc_RDC", "password": "Dc_RDC"}
        r = requests.post(url=url, json=param).json()
        return r['data']['token']
    else:
        url = 'https://goat-dev-api.geehoop.com/auth/login'
        param = {"mobile": "Dc_RDC", "password": "Dc_RDC"}
        r = requests.post(url=url, json=param).json()
        return r['data']['token']


def update_email_and_resend(goatOrderNo, new):
    """更新自提订单邮箱号，重新入库发送邮件，更正入库时间"""
    if w1.stocks.find_one({"goatOrderNo": goatOrderNo}) is not None:
        if 'shelfId' not in w1.stocks.find_one({"goatOrderNo": goatOrderNo}):
            w1.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"email": new}})
            print('订单暂未入库')
        else:
            url = 'https://w1-api.hkmotivate.com/stock/in'
            header = {"Content-Type": "application/json", "Authorization": f"Bearer {get_token('w1')}"}
            instocktime = w1.stocks.find_one({"goatOrderNo": goatOrderNo})['inStockTime']
            shelfcode = w1.stocks.find_one({"goatOrderNo": goatOrderNo})['shelfId']
            uniqueCode = w1.stocks.find_one({"goatOrderNo": goatOrderNo})['uniqueCode']
            w1.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"status": "waitingInStock", "email": new}})
            param = {"list": [{"shelfId": shelfcode, "uniqueCode": str(uniqueCode)}]}
            res = requests.post(url=url, headers=header, json=param)
            print('w1', instocktime)
            print(res.json())
            w1.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"inStockTime": instocktime}})
    elif w2.stocks.find_one({"goatOrderNo": goatOrderNo}) is not None:
        if 'shelfId' not in w2.stocks.find_one({"goatOrderNo": goatOrderNo}):
            w2.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"email": new}})
            print('订单暂未入库')
        else:
            url = 'https://w2-api.hkmotivate.com/stock/in'
            header = {"Content-Type": "application/json", "Authorization": f"Bearer {get_token('w2')}"}
            instocktime = w2.stocks.find_one({"goatOrderNo": goatOrderNo})['inStockTime']
            shelfcode = w2.stocks.find_one({"goatOrderNo": goatOrderNo})['shelfId']
            uniqueCode = w2.stocks.find_one({"goatOrderNo": goatOrderNo})['uniqueCode']
            w2.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"status": "waitingInStock", "email": new}})
            param = {"list": [{"shelfId": shelfcode, "uniqueCode": str(uniqueCode)}]}
            res = requests.post(url=url, headers=header, json=param)
            print('w1', instocktime)
            print(res.json())
            w2.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"inStockTime": instocktime}})
    else:
        print('订单号不存在')
        pass


def resend_mail(goatOrderNo):
    """重新发邮件"""
    if w1.stocks.find_one({"goatOrderNo": goatOrderNo, "status": "waitingPay"}) is not None:
        url = 'https://w1-api.hkmotivate.com/stock/in'
        header = {"Content-Type": "application/json", "Authorization": f"Bearer {get_token('w1')}"}
        res = w1.stocks.find_one({"goatOrderNo": goatOrderNo, "status": "waitingPay"})
        instocktime = res['inStockTime']
        email = res['email']
        shelfcode = res['shelfId']
        uniqueCode = res['uniqueCode']
        w1.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"status": "waitingInStock"}})
        param = {"list": [{"shelfId": shelfcode, "uniqueCode": str(uniqueCode)}]}
        result = requests.post(url=url, headers=header, json=param)
        print('w1', instocktime, email)
        print(result.json())
        w1.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"inStockTime": instocktime}})
    elif w2.stocks.find_one({"goatOrderNo": goatOrderNo, "status": "waitingPay"}) is not None:
        url = 'https://w2-api.hkmotivate.com/stock/in'
        header = {"Content-Type": "application/json", "Authorization": f"Bearer {get_token('w2')}"}
        res = w2.stocks.find_one({"goatOrderNo": goatOrderNo, "status": "waitingPay"})
        instocktime = res['inStockTime']
        shelfcode = res['shelfId']
        email = res['email']
        uniqueCode = res['uniqueCode']
        w2.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"status": "waitingInStock"}})
        param = {"list": [{"shelfId": shelfcode, "uniqueCode": str(uniqueCode)}]}
        result = requests.post(url=url, headers=header, json=param)
        print('w2', instocktime, email)
        print(result.json())
        w2.stocks.update_one({"goatOrderNo": goatOrderNo}, {"$set": {"inStockTime": instocktime}})
    else:
        print('订单号不存在')
        pass


# update_email_and_resend('109300722', 'wu694679727@163.com')
resend_mail('735568634')
# print(get_token('w1'))
